<?php
require_once 'net/crashtoot/php/picasa/viewer/PicasaHandler.class.php';
require_once 'net/crashtoot/php/picasa/model/Album.class.php';
require_once 'net/crashtoot/php/picasa/model/Photo.class.php'; 
require_once 'AbstractModel.class.php';   

/**
 * Utilisateur Picasa.
 */
class PicasaUser {
	
	private $handler;
	
	public $login;
	public $username;
	public $password;

	public $nickname = null;
	public $avatar;
	
	public $quotaLimit;
	public $quotaCurrent;
	public $maxPhotosPerAlbum;

	private $connected = false;

	// cache des albums
	private $albums = array();
	// cache des photos par albums
	private $photos = array();
	
	public static function createPicasaUser($username, $login, $password) {
  	    $user = new PicasaUser();
  	
    		$user->username = $username;
    		$user->login = $login;
    		$user->password = $password;
    		
    		return $user;
  	}
	
  public function setTableDefinition() {           
    $this->hasColumn('login', 'integer', 4, array("primary" => true));     
    $this->hasColumn('username', 'string', 255, array("notnull" => true));
    $this->hasColumn('password', 'string', 50, array("notnull" => true));
    $this->hasColumn('nickname', 'string', 255, array("notnull" => true));  
    $this->hasColumn('avatar', 'string', 255, array("notnull" => true));
    $this->hasColumn('quotaLimit', 'string', 255, array("notnull" => true));                      
    $this->hasColumn('quotaCurrent', 'string', 10, array("notnull" => true));
    $this->hasColumn('maxPhotosPerAlbum', 'integer', 1, array("notnull" => true));
  }

	public function connect() {
		$this->handler = new PicasaHandler();
		$this->connected = $this->handler->connect($this->username, $this->login, $this->password);

		if ($this->nickname == null) {
			$this->initUserInfos();
		}
	}

	public function disconnect() {
		$this->handler = null;
		$this->connected = false;
	}

	public function initUserInfos() {
		$query = new Zend_Gdata_Photos_UserQuery();
		$query->setUser($this->username);
		$userFeed = $this->getHandler()->getService()->getUserFeed(null, $query);

		$this->nickname = $userFeed->getGphotoNickname();
		$this->avatar = $userFeed->getGphotoThumbnail();
		
		$extensions = $userFeed->getExtensionElements();
		$this->quotaLimit = $extensions[0]->text / 1024 / 1024;
		$this->quotaCurrent = $extensions[1]->text / 1024 / 1024;
		$this->maxPhotosPerAlbum = $extensions[2]->text;
	}

	public function isConnected() {
		return $this->connected;
	}

	public function getAlbums() {
		if (sizeof($this->albums) == 0) {
			$this->albums = $this->getHandler()->getAlbums($this->username, true);
		}
		return $this->albums;
	}

	public function getAlbum($albumIdSearch) {
		foreach ($this->getAlbums() as $albumId => $album) {
			if ($albumIdSearch == $albumId) {
				return $album;
			}
		}
		return null;
	}

	public function getPictures($albumId) {
		$photos = $this->photos[$albumId];
		if ($photos == null) {
			$photos = $this->getHandler()->getPictures($this->username, $albumId);
			$this->photos[$albumId] = $photos;
		}
		return $photos;
	}
	
	public function getPicture($albumId, $photoId) {
		$photos = $this->getPictures($albumId);
		if ($photos != null) {
			return $photos[$photoId];
		} else {
			return null;
		}
	}

	public function getHandler() {
		if ($this->handler == null) {
			$this->connect();
		}
		return $this->handler;
	}

	public function getUsername() {
		return $this->username;
	}

	public function getLogin() {
		return $this->login;
	}

	public function getAvatar() {
		return $this->avatar;
	}

	public function getNickname() {
		return $this->nickname;
	}

	public function outputAlbums() {
		$this->getHandler()->outputAlbums($this->getAlbums());
	}

	public function outputUser() {
		$this->getHandler()->outputUser($this->username);
	}

	public function getQuotaLimit() {
		return round($this->quotaLimit, 2);
	}

	public function getQuotaCurrent() {
		return round($this->quotaCurrent, 2);
	}

	public function getQuotaPercent() {
		return round($this->quotaCurrent * 100 / $this->quotaLimit, 2);
	}
	
	public function getQuotaMessage() {
		return "Vous utilisez actuellement ".round($this->getQuotaCurrent(), 0)."Mo (".$this->getQuotaPercent()."%) de vos ".$this->getQuotaLimit()."Mo.";
	}
}
?>